/*
 * Copyright (c) 2010-2011 Josef Hardi <josef.hardi@gmail.com>
 * Copyright (c) 2010 Piotr Zagawa <piotr.zagawa@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package id.duapuluhdua.dakidaki;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.MotionEvent;

import id.duapuluhdua.dakidaki.activities.ActivityMain;
import id.duapuluhdua.dakidaki.common.CommonActivity;
import id.duapuluhdua.dakidaki.compass.CompassRotation;
import id.duapuluhdua.dakidaki.layers.LayerMap;
import id.duapuluhdua.dakidaki.layers.LayerStats;
import id.duapuluhdua.dakidaki.layers.SetOfLayers;
import id.duapuluhdua.dakidaki.map.Screen;
import id.duapuluhdua.dakidaki.ui.UIScreen;

/**
 * This class provides the base for drawing the whole elements of the
 * application.
 */
public class MainDraw
{
  /** Fields */
  protected CommonActivity mParent;
  private Screen mScreen;
  private LayerMap mMap;
  private SetOfLayers mLayers;
  private LayerStats mStats;
  private UIScreen mUi;

  public static CompassRotation compassRotation = null;

  /**
   * The constructor.
   *
   * @param parent the main user interaction handler.
   */
  public MainDraw(CommonActivity parent)
  {
    mParent = parent;
    mScreen = ActivityMain.loader.getScreen();

    // Create shared compass (an observer)
    compassRotation = new CompassRotation(parent);

    mMap = new LayerMap(parent, mScreen);
    mLayers = new SetOfLayers(parent, mScreen);
    mUi = new UIScreen(parent, mScreen);
  }

  /**
   * Initialize all the layers. The call is done before the animation loop
   * starts.
   */
  public void initData()
  {
    mLayers.initData();
  }

  /**
   * Handle the drawing sequence of the whole graphical elements, such as
   * the screen grids, the tracks, the layers (e.g., compass, location, info),
   * the UI elements, and statistic information.
   *
   * @param canvas
   */
  public void doDraw(Canvas canvas)
  {
    final Rect area = canvas.getClipBounds();  // the screen dimension.

    // Set the screen size in surfaceSizeChanged(..)
    synchronized (mScreen)
    {
      mScreen.setSize(area.width(), area.height());
    }

    // Draw map
    mMap.doDraw(canvas, area);

    // Draw data
//    ActivityMain.loader.doDraw(canvas, area);

    // Draw layers
    mLayers.doDraw(canvas, area);

    // Draw UI elements
    mUi.doDraw(canvas);

    // Draw statistic information
    if (mStats != null) {
      mStats.doDraw(canvas, area);
    }

    // Update shared compass data
    compassRotation.updateState(mScreen, canvas, area);
  }

  /**
   * Update the layers and UI objects state.
   */
  public void updateObjectsState()
  {
    mLayers.updateObjectsState();
    mUi.updateObjectsState();
  }

  /**
   * Updates the new size to all objects, images.
   *
   * @param width
   *          the new screen width.
   * @param height
   *          the new screen height.
   * @param screenOrientation
   *          the new screen orientation.
   */
  public void surfaceSizeChanged(int width, int height, int screenOrientation)
  {
    mLayers.surfaceSizeChanged(width, height, screenOrientation);
    mUi.surfaceSizeChanged(width, height, screenOrientation);
    ActivityMain.loader.surfaceSizeChanged(width, height, screenOrientation);
  }

  public boolean onDownEvent(MotionEvent e)
  {
    final float x = e.getX();
    final float y = e.getY();

    mMap.registerTouchPoint(x, y);
    mMap.postLongPressCallback();

    return true;
  }

  public boolean onActionUpEvent(MotionEvent e)
  {
    mMap.removeLongPressCallback();

    return true;
  }

  public boolean onScrollEvent(float dx, float dy)
  {
    mMap.removeLongPressCallback();
    mMap.onScroll(dx, dy);

    return true;
  }
}
